install.packages("arules")
library(arules)
library(arulesViz)
data("Groceries")
summary(Groceries)


itemFrequencyPlot(Groceries)
itemFrequencyPlot(Groceries[,1:30], col='lightblue',horiz=T)
itemFrequencyPlot(Groceries[,1:30], col='lightblue',horiz=T, topN=15, main='zcdzg15')



apriori(Groceries)
Groceries.ar1<-apriori(Groceries, parameter = list(support=0.001, confidence=0.5))
Groceries.ar1
inspect(Groceries.ar1)
inspect(head(sort(Groceries.ar1, by='lift'),8))


plot(Groceries.ar1, measure = c("support", "confidence"), shading = 'lift')
plot(Groceries.ar1, shading = "order", control = list(main="Two-key plot"))


Groceries.ar1sr<-subset(Groceries.ar1, subset=confidence>0.8)
Groceries.ar1sr

plot(Groceries.ar1sr, method = "matrix", measure = "lift")
plot(Groceries.ar1sr, method = "matrix", measure = c("lift", "confidence"))


plot(Groceries.ar1sr, method="paracoord")


plot(Groceries.ar1sr, method = "matrix3D", measure = "lift")
plot(Groceries.ar1sr, method = "matrix3D", measure = "lift", control = list(reorder=TRUE))


plot(Groceries.ar1sr, method="grouped", control = list(k=10))




data(Titanic)

library(epitools)


Titanic.df<- expand.table(Titanic)
Titanic.tr<- as(Titanic.df, "transactions")
head(Titanic.df)


Titanic.im<- as(Titanic.tr, "itemMatrix")
Titanic.ar<- apriori(Titanic.tr)

inspect(head(sort(Titanic.ar, by="lift"),5))
inspect(subset(Titanic.ar,subset=rhs %in% "Survived=Yes"|rhs %in% "Survived=No"))

Titanic.ar2<-apriori(Titanic.tr, parameter = list(support=0.005, confidence=0.8, minlen=2), 
                     appearance = list(rhs=c("Survived=No", "Survived=Yes"), default="lhs"), control=list(verbose=F))
Titanic.ar2
plot(Titanic.ar2, method="grouped", control=list(k=6))
plot(Titanic.ar2, method="graph")

plot(Titanic.ar2, method="paracoord")

write.csv(as(Titanic.ar2, "data.frame"), "TitanicAr2.csv")
